package com.patrol.controller;

import com.patrol.dao.TaskPointDao;
import com.patrol.pojo.TaskPoint;
import com.patrol.pojo.User;
import com.patrol.pojo.UserTask;
import com.patrol.service.TaskPointService;
import com.patrol.service.UserService;
import com.patrol.service.WorkerService;
import com.patrol.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Handler;

/**
 * @author zsc
 * @description: 点位管理接口
 */

@Api(tags = "点位管理接口")
@RestController
@RequestMapping("/api/taskpoint")
@CrossOrigin
public class TaskPointController {
    @Resource
    private TaskPointService taskPointService;

    @Resource
    private TaskPointDao taskPointDao;

    @Resource
    private UserService userService;

    @Resource
    private WorkerService workerService;

    /**
     * 根据userId、taskId删除用户被分配的任务点
     * @param userId,taskId
     * @return
     */
    @ApiOperation(value = "删除用户被分配的任务点")
    @GetMapping("delUserTask")
    public Result delUserTask(@RequestParam(value = "userId") String userId,
                              @RequestParam(value = "taskId") String taskId){
        Result result = new Result();
        boolean b = taskPointService.delUserTask(userId, taskId);
        List<Integer> pointIds = taskPointService.findPointIds(Integer.parseInt(userId));
        if (pointIds.isEmpty()){
            userService.changeUserKXState(userId);
            result.setCode(200);
            return result;
        }
        result.setCode(200);
        return result;
    }


    /**
     * 根据userId查询指定巡更人员被分配的任务点位及完成状态
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询指定巡更人员被分配的任务点位信息")
    @GetMapping("findUserPoints/{userId}")
    public Result findUserPoints(@PathVariable int userId){
        //查询该用户被分配的所有任务点ID
        List<Integer> pointIds =  taskPointService.findPointIds(userId);
        //查询这些任务点位的信息
        List<TaskPoint> list = new ArrayList<>();
        for (int i=0;i<pointIds.size();i++){
            //该点位状态
            int taskState = taskPointService.selectTaskState(pointIds.get(i), userId);
            TaskPoint taskPoint = taskPointService.selectPointById(pointIds.get(i));
            Map<String,Object> map = new HashMap<>();
            map.put("taskState", taskState);
            taskPoint.setParam(map);
            list.add(taskPoint);
        }
        return Result.ok(list);
    }

    /**
     * 分配任务点位
     * @param
     * @return
     */
    @ApiOperation(value = "分配任务点位")
    @PostMapping("allotPoint")
    public Result allotPoint(@RequestParam(value = "users")List<String> users,
                             @RequestParam(value = "points")List<String> points){
        Result result = new Result();
        List<String> users11 = new ArrayList<>();
        List<String> points11 = new ArrayList<>();
        for (int i=0;i<users.size()-1;i++){
            users11.add(users.get(i));
        }

        for (int j=0;j<points.size()-1;j++){
            points11.add(points.get(j));
        }

        for (int i=0;i<users11.size();i++){
            workerService.createUserTask(Integer.parseInt(users.get(i)));
            User user = userService.selectUserById(users11.get(i));
            if (user.getIsAssigned()==1){
                result.setMessage("该巡更人员已被派遣！！");
                result.setCode(203);
                break;
            }
            for (int j=0;j<points11.size();j++){
                //查看该巡更人员是否已被分配该点位
                boolean b = taskPointService.selectUserPoint(users.get(i), points.get(j));
                if (b){
                    workerService.updateUserTaskNum(Integer.parseInt(users.get(i)));
                    taskPointService.allotPoint(users.get(i), points.get(j));
                    //更改巡更人员状态为已被派遣
                    userService.changeUserState(users.get(i));
                    //更改任务点状态及任务点分配时间
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    taskPointService.changePointState(points.get(j), df.format(new Date()));
                    result.setMessage("派遣成功");
                    result.setCode(200);
                }else {
                    result.setMessage("该任务点已经派遣！！");
                    result.setCode(202);
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 添加点位
     * @param taskPoint
     * @return
     */
    @ApiOperation(value = "添加点位")
    @PostMapping("insertPoint")
    public Result insertPoint(@RequestBody TaskPoint taskPoint){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taskPoint.setCreateTime(df.format(new Date()));
        taskPoint.setUpdateTime(df.format(new Date()));
        boolean flag = taskPointService.save(taskPoint);
        if (flag) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据ID查询点位
     * @param taskId
     * @return
     */
    @ApiOperation(value = "根据ID查询点位")
    @GetMapping("selectPointById/{taskId}")
    public Result selectPointById(@PathVariable Integer taskId){
        TaskPoint taskPoint = taskPointService.selectPointById(taskId);
        return Result.ok(taskPoint);
    }

    /**
     * 根据taskName查询点位
     * @param taskName
     * @return
     */
    @ApiOperation(value = "根据taskName查询点位")
    @GetMapping("selectPointByName/{taskName}")
    public Result selectPointByName(@PathVariable String taskName){
        TaskPoint taskPoint = taskPointService.selectPointByName(taskName);
        List<TaskPoint> list = new ArrayList<>();
        list.add(taskPoint);
        return Result.ok(list);
    }

    /**
     * 查询所有点位
     * @return
     */
    @ApiOperation(value = "查询所有点位")
    @GetMapping("selectAll")
    public Result selectAll(){
        List<TaskPoint> list = taskPointService.selectAllPoint();
        return Result.ok(list);
    }

    /**
     * 根据ID逻辑删除点位
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID逻辑删除点位")
    @DeleteMapping("{id}")
    public Result deleteOneById(@PathVariable Long id){
        boolean flag = taskPointService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 批量逻辑删除点位
     * @param idList
     * @return
     */
    @ApiOperation(value = "批量逻辑删除点位")
    @DeleteMapping("batchRemove")
    public Result batchRemovePoint(@RequestBody List<Long> idList){
        taskPointService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 根据ID修改点位信息
     * @param taskPoint
     * @return
     */
    @ApiOperation(value = "根据ID修改点位信息")
    @PostMapping("updatePoint")
    public Result updatePointById(@RequestBody TaskPoint taskPoint){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taskPoint.setUpdateTime(df.format(new Date()));
        boolean flag = taskPointService.updateById(taskPoint);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "巡更人员定位打卡任务点")
    @GetMapping("clockPoint/{userId}/{taskId}")
    public Result userClockRWPoint(@PathVariable("userId") int userId,
                                   @PathVariable("taskId") int taskId){
        boolean flag = taskPointService.clockPoint(taskId, userId);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}
